/*
 * Copyright (c) 2025 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef __HMFS_QUOTA_H__
#define __HMFS_QUOTA_H__

#include <memory>
// #include "hmfs_define.h"

namespace OHOS::Hmfs {

namespace {
/*
 * Definitions of magics and versions of current quota files
 */
constexpr std::array<uint32_t, 3> INIT_QUOTA_MAGICS = {
    0xd9c01f11, /* USRQUOTA */
    0xd9c01927, /* GRPQUOTA */
    0xd9c03f14  /* PRJQUOTA */
};



}


enum quota_type {
    USRQUOTA = 0,
    GRPQUOTA = 1,
    PRJQUOTA = 2,
    MAXQUOTAS = 3,
};

#define QUOTA_USR_BIT (1 << USRQUOTA)
#define QUOTA_GRP_BIT (1 << GRPQUOTA)
#define QUOTA_PRJ_BIT (1 << PRJQUOTA)
#define QUOTA_ALL_BIT (QUOTA_USR_BIT | QUOTA_GRP_BIT | QUOTA_PRJ_BIT)

#define QUOTA_DATA_BLOCK_COUNT      2

#define MAX_IQ_TIME  604800 /* (7*24*60*60) 1 week */
#define MAX_DQ_TIME  604800 /* (7*24*60*60) 1 week */

#define QT_TREEOFF    1    /* Offset of tree in file in blocks */
#define DEFAULT_PROJECT_ID          0    /* default project ID */


struct DiskQuotaHeader {
    uint32_t dqhMagic;     /* Magic number identifying file */
    uint32_t dqhVersion;   /* File version */
};

/* Header with type and version specific information */
struct DiskQuotaInfo {
    uint32_t dqiBgrace;    /* Time before block soft limit becomes hard limit */
    uint32_t dqiIgrace;    /* Time before inode soft limit becomes hard limit */
    uint32_t dqiFlags;    /* Flags for quotafile (DQF_*) */
    uint32_t dqiBlocks;    /* Number of blocks in file */
    uint32_t dqiFreeBlk;    /* Number of first free block in the list */
    uint32_t dqiFreeEntry;    /* Number of block with at least one free entry */
};

struct DiskQuotaBlock {
    uint32_t dqbId;      /* id this quota applies to */
    uint32_t dqbPad;
    uint64_t dqbIhardlimit;  /* absolute limit on allocated inodes */
    uint64_t dqbIsoftlimit;  /* preferred inode limit */
    uint64_t dqbCurinodes;   /* current # allocated inodes */
    uint64_t dqbBhardlimit;  /* absolute limit on disk space
                 * (in QUOTABLOCK_SIZE) */
    uint64_t dqbBsoftlimit;  /* preferred limit on disk space
                 * (in QUOTABLOCK_SIZE) */
    uint64_t dqbCurspace;    /* current space occupied (in bytes) */
    uint64_t dqbBtime;       /* time limit for excessive disk use */
    uint64_t dqbItime;       /* time limit for excessive inode use */
};


} // namespace OHOS::Hmfs
#endif